package com.sf.osms.dep.processor.mail;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.sf.osms.dep.cache.SysConfigCacheHelper;
import com.sf.osms.dep.processor.util.DESUtils;
import com.sf.osms.dep.processor.util.ExceptionUtils;

public class SendMailUtil {

	public static final String MAIL_HOST = "mail.sf-express.com";
    public static final String MAIL_PORT = "25";
    public static final String USER_NAME = "kamd";
    public static final String PASSWORD = "Yy19900624";
    public static final String MAIL_FROM = "kamd@sf-express.com";
	private static final String MAIL_TO = "sfhq1405@sf-express.com";
	private final static Logger logger = Logger.getLogger(SendMailUtil.class);
	/**
	 * @MethodName: sendMail
	 * @Description: 邮件发送
	 * @param title
	 *            主题
	 * @param content
	 *            内容
	 * @param mailTo
	 *            收件人
	 * @return void 返回类型
	 * @throws
	 */

	
	public static void sendMail(String title, String content, String mailTo) {
		
	    if(StringUtils.isBlank(title) || StringUtils.isBlank(content)){
	    	return ;
	    }
		// 设置邮件服务器信息
		MailSender mailInfo = new MailSender();
		mailInfo.setMailServerHost(getMailInfo("dep.mail.host", MAIL_HOST));
		mailInfo.setMailServerPort(getMailInfo("dep.mail.port", MAIL_PORT));
		mailInfo.setValidate(true);

		// 邮箱用户名
		mailInfo.setUserName(getMailInfo("dep.mail.username", USER_NAME));
		// 邮箱密码
		//String password = DESUtils.decrypt(PASSWORD);
		mailInfo.setPassword(getMailInfo("dep.mail.password", PASSWORD));
		// 发件人邮箱
		mailInfo.setFromAddress(getMailInfo("dep.mail.from", MAIL_FROM));
		// 收件人邮箱
		if(null == mailTo){
			mailTo = getMailInfo("dep.mail.to", MAIL_TO);
		}
		mailInfo.setToAddress(mailTo);
		// 邮件标题
		mailInfo.setSubject(title);
		// 邮件内容
		mailInfo.setContent(content);
		// 发送html格式
		try {
			SimpleMailSender.sendHtmlMail(mailInfo);
		} catch (Exception e) {
			logger.error("*****发送邮件失败" + ExceptionUtils.getExceptionStackString(e)+ "*****");
		}
		
	}
	
	public static void sendMail(String title, String content, String mailTo,Map<String,String> imgNamePathMap) {

		if(StringUtils.isBlank(title) || StringUtils.isBlank(content) || null == imgNamePathMap){
	    	return ;
	    }
		// 设置邮件服务器信息
		MailSender mailInfo = new MailSender();
		mailInfo.setMailServerHost(getMailInfo("dep.mail.host", MAIL_HOST));
		mailInfo.setMailServerPort(getMailInfo("dep.mail.port", MAIL_PORT));
		mailInfo.setValidate(true);

		// 邮箱用户名
		mailInfo.setUserName(getMailInfo("dep.mail.username", USER_NAME));
		// 邮箱密码
		//String password = DESUtils.decrypt(PASSWORD);
		mailInfo.setPassword(getMailInfo("dep.mail.password", PASSWORD));
		// 发件人邮箱
		mailInfo.setFromAddress(getMailInfo("dep.mail.from", MAIL_FROM));
		// 收件人邮箱
		if(null == mailTo){
			mailTo = getMailInfo("dep.mail.to", MAIL_TO);
		}
		mailInfo.setToAddress(mailTo);
		// 邮件标题
		mailInfo.setSubject(title);
		// 邮件内容
		mailInfo.setContent(content);
		
		mailInfo.setImgNamePathMap(imgNamePathMap);
		// 发送html格式
		
		try {
			SimpleMailSender.sendHtmlMail(mailInfo);
		} catch (Exception e) {
			logger.error("*****发送邮件失败" + ExceptionUtils.getExceptionStackString(e)+ "*****");
		}
	}
	
	/**
	 * 
	* @MethodName: sendMailWithAttachment
	* @Description: 发送带附件的邮件
	* @param title 邮件标题
	* @param content 邮件内容
	* @param mailTo 收件人地址
	* @param fileNames   附件的绝对路径     数组列表 
	* @return void    返回类型
	* @throws
	 */
	public static void sendMailWithAttachment(String title, String content, String mailTo, String[] fileNames) {
		
		if(StringUtils.isBlank(title) || StringUtils.isBlank(content) || null == fileNames){
	    	return ;
	    }
		// 设置邮件服务器信息
		MailSender mailInfo = new MailSender();
		mailInfo.setMailServerHost(getMailInfo("dep.mail.host", MAIL_HOST));
		mailInfo.setMailServerPort(getMailInfo("dep.mail.port", MAIL_PORT));
		mailInfo.setValidate(true);

		// 邮箱用户名
		mailInfo.setUserName(getMailInfo("dep.mail.username", USER_NAME));
		// 邮箱密码
		//String password = DESUtils.decrypt(PASSWORD);
		mailInfo.setPassword(getMailInfo("dep.mail.password", PASSWORD));
		// 发件人邮箱
		mailInfo.setFromAddress(getMailInfo("dep.mail.from", MAIL_FROM));
		// 收件人邮箱
		if(null == mailTo){
			mailTo = getMailInfo("dep.mail.to", MAIL_TO);
		}
		mailInfo.setToAddress(mailTo);
		// 邮件标题
		mailInfo.setSubject(title);
		// 邮件内容
		mailInfo.setContent(content);
		
		mailInfo.setAttachFileNames(fileNames);
		// 发送html格式
	
		try {
			SimpleMailSender.sendHtmlMail(mailInfo);
		} catch (Exception e) {
			logger.error("*****发送邮件失败" + ExceptionUtils.getExceptionStackString(e)+ "*****");
		}
	}
	
	public static String getMailInfo(String key,String defaultVal){
		/*Properties prop = AppContext.getContext().getConfigProperties();
		String value = (String) prop.get(key.trim());*/
		String value = SysConfigCacheHelper.getSysConfigValue(key.trim());
		if(StringUtils.isEmpty(value)){
			return defaultVal;
		}
		
		return value.trim();
	}
	public static void main(String[] args) {
		System.out.println(DESUtils.decrypt(PASSWORD));
		sendMail("测试", "邮件来了吗？", "sfhq1405@sf-express.com");
	}
}
